package com.aos.erp.manager.rest;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.manager.model.sys.dto.UserDto;
import com.aos.erp.manager.remote.MessageService;
import com.aos.erp.manager.remote.model.MTParamInfo;
import com.aos.erp.manager.remote.model.dto.MessageRecordItemDTO;
import com.aos.erp.manager.remote.model.dto.MessageRecordWaitPushItemDTO;
import com.aos.erp.manager.remote.model.dto.MessageTemplateDTO;
import com.aos.erp.manager.remote.model.dto.MessageTemplateItemDTO;
import com.aos.erp.manager.remote.model.vo.MRConditionVo;
import com.aos.erp.manager.remote.model.vo.MTConditionVo;
import com.aos.erp.manager.remote.model.vo.MessageTemplateVo;
import com.aos.erp.manager.util.UserUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = {"UI接口：消息接口"})
@RestController
@RequestMapping("/api/message")
public class RestMessageController {

    @Resource
    private MessageService messageService;

    @GetMapping("/param/listAll")
    @ApiOperation(value = "获取所有模板参数")
    public ResultEntity<List<MTParamInfo>> queryAll() {
        return ResultEntity.success(messageService.queryAllMTParam());
    }

    @ApiOperation(value = "保存消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mtTitle", value = "模板标题(100字符内)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pushTypeList", value = "推送方式数组[{mtContent:'消息模板(1000字符内，APP、短信推送需要)',mtWxTemplateId:'微信模板ID,微信公众号、微信小程序推送需要',mtUrl:'跳转链接,微信公众号、微信小程序推送需要，其中小程序仅限小程序内的页面;可不传,点击后无跳转',pushType:推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；sms:短信),pushParam:[{mtpId:参数项ID,mtpItem:'参数项', mtpDesc:'参数项描述'}] }]", required = true, dataType = "List"),
            @ApiImplicitParam(name = "mtDriverSchoolList", value = "模板下发驾校数组[{dsId:0,dsCode:'驾校编号',dsName:'驾校名称'}]", required = true, dataType = "List"),
    })
    @PostMapping("/template/add")
    public ResultEntity addTemplate(@ApiParam("入参") @RequestBody MessageTemplateVo params, String currentUserId) throws Exception{

        UserDto userDto = UserUtil.getCurrentUserInfo(currentUserId);
        params.setOptUser(userDto.getName());
        messageService.addTemplate(params);
        return ResultEntity.success("新增成功");
    }

    @ApiOperation(value = "编辑消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模板ID", required = true, dataType = "Long", paramType = "Query"),
            @ApiImplicitParam(name = "mtTitle", value = "模板标题(100字符内)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pushTypeList", value = "推送方式数组[{mtContent:'消息模板(1000字符内，APP、短信推送需要)',mtWxTemplateId:'微信模板ID,微信公众号、微信小程序推送需要',mtUrl:'跳转链接,微信公众号、微信小程序推送需要，其中小程序仅限小程序内的页面;可不传,点击后无跳转',pushType:推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；sms:短信)}]", required = true, dataType = "List"),
            @ApiImplicitParam(name = "mtDriverSchoolList", value = "模板下发驾校数组[{dsId:0,dsCode:'驾校编号',dsName:'驾校名称'}]", required = true, dataType = "List"),
    })
    @PostMapping("/template/update")
    public ResultEntity updateTemplate(@ApiParam(value = "入参") @RequestBody MessageTemplateVo params, String currentUserId) throws Exception{

        UserDto userDto = UserUtil.getCurrentUserInfo(currentUserId);
        params.setOptUser(userDto.getName());
        messageService.updateTemplate(params);
        return ResultEntity.success("修改成功");
    }

    @ApiOperation(value = "分页查询消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", defaultValue = "1", paramType = "Query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "Integer", defaultValue = "20", paramType = "Query"),
            @ApiImplicitParam(name = "mtCode", value = "模板编号", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "mtTitle", value = "模板标题", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "pushType", value = "推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；sms:短信)", dataType = "PushType", paramType = "Query"),
            @ApiImplicitParam(name = "fromTime", value = "开始时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "toTime", value = "截止时间", dataType = "String", paramType = "Query")
    })
    @GetMapping("/template/list")
    public ResultEntity<PageBean<MessageTemplateItemDTO>> listTemplate(MTConditionVo params) throws Exception {
        return ResultEntity.success(messageService.queryTemplateForPage(params));
    }


    @ApiOperation(value = "查看模板详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId", value = "模板ID", required = true, dataType = "Long", paramType = "Query")
    })
    @GetMapping("/template/detail")
    public ResultEntity<MessageTemplateDTO> detailTemplate(Long dataId){

        // 获取消息模板
        MessageTemplateDTO messageTemplateDTO = messageService.getMessageTemplateDetail(dataId);
        return ResultEntity.success(messageTemplateDTO);
    }

    @ApiOperation(value = "删除消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "记录ID：id1,id2,id3", required = true, dataType = "Long", paramType = "Query")
    })
    @GetMapping("/template/delete")
    public ResultEntity deleteTemplate(String ids) throws Exception {
        return messageService.deleteTemplate(ids);
    }

    @ApiOperation(value = "删除消息记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "记录ID：id1,id2,id3", required = true, dataType = "Long", paramType = "Query")
    })
    @GetMapping("/record/delete")
    public ResultEntity deleteRecord(String ids) throws Exception{
        return messageService.deleteRecord(ids);
    }

    @ApiOperation(value = "查询推送记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", defaultValue = "1", paramType = "Query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "Integer", defaultValue = "20", paramType = "Query"),
            @ApiImplicitParam(name = "mrContent", value = "推送内容", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "pushType", value = "推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；)", dataType = "PushType", paramType = "Query"),
            @ApiImplicitParam(name = "pushStatus", value = "推送状态：push_no为未发送；push_yes为已发送；push_stop为暂停发送", dataType = "PushStatus", paramType = "Query"),
            @ApiImplicitParam(name = "fromTime", value = "发送开始时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "toTime", value = "发送截止时间", dataType = "String", paramType = "Query")
    })
    @GetMapping("/record/listPushRecord")
    public ResultEntity<PageBean<MessageRecordItemDTO>> listPushRecord(MRConditionVo params) throws Exception {
        return ResultEntity.success(messageService.queryRecordForPage(params));
    }

    @ApiOperation(value = "查询推送记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", defaultValue = "1", paramType = "Query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "Integer", defaultValue = "20", paramType = "Query"),
            @ApiImplicitParam(name = "mrContent", value = "推送内容", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "pushType", value = "推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；)", dataType = "PushType", paramType = "Query"),
            @ApiImplicitParam(name = "pushStatus", value = "推送状态：push_no为未发送；push_yes为已发送；push_stop为暂停发送", dataType = "PushStatus", paramType = "Query"),
            @ApiImplicitParam(name = "fromTime", value = "发送开始时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "toTime", value = "发送截止时间", dataType = "String", paramType = "Query")
    })
    @GetMapping("/record/listWaitPushRecord")
    public ResultEntity<PageBean<MessageRecordWaitPushItemDTO>> listWaitPushRecord(MRConditionVo params) throws Exception {
        return ResultEntity.success(messageService.queryWaitPushRecordForPage(params));
    }
}
